package com.haikesi.api.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.haikesi.api.base.*;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.mode.domain.po.PlatformModePO;
import com.haikesi.api.platform.domain.po.PlatformParamPo;
import com.haikesi.api.platform.domain.vo.PlatformParamSearchVo;
import com.haikesi.api.platform.domain.vo.PlatformParamUpdateVo;
import com.haikesi.api.platform.service.IPlatformParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @version V1.0
 * @ClassName: PlatformParamController
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: zqy
 * @date: 2019年5月14日 下午4:21:39
 * @Copyright: 2019 重庆海克斯科技有限公司.
 * 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Api(description = "平台参数配置相关接口", tags = ApiIndex.PlatformParamController)
@RequestMapping(value = "/api/platformparam")
@RestController
public class PlatformParamController {
    private static Log log = LogFactory.getLog(PlatformParamController.class);
    @Autowired
    private IPlatformParamService platformParamService;

    // key生成器,redis生成key必须用
    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "查询列表")
    @GetMapping(value = "/list")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R<List<PlatformParamSearchVo>> list(@RequestParam String platformType) {
        List<PlatformParamSearchVo> list = platformParamService.getList(platformType);
        return new R(list);
    }


    @ApiOperation(value = "平台参数更改")
    @PutMapping(value = "")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R update(
            @ApiParam(value = "传入参数") @Valid @RequestBody PlatformParamUpdateVo platformParamUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        platformParamService.update(platformParamUpdateVo);
        return new R();
    }


    @ApiOperation(value = "平台回调接口")
    @PostMapping(value = "callback_operation")
    public String callback_operation(
            @ApiParam(value = "传入参数") @RequestBody HashMap<String, Object> params) {
        log.info(">>>>>>>>>>>处理平台回调......");
        log.info(">>>>>>>>>>>处理平台回调......");
        return platformParamService.callback_operation(params);
    }

    @ApiOperation(value = "将平台配置3D信息返回的组织机构id保存到Redis")
    @GetMapping(value = "/addRedis")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R addRedis(String back_3D, String url_3D, String organization_3D, String organizationName_3D, String org_id) {
        JSONObject json = new JSONObject();
        json.put("back_3D", back_3D);
        json.put("url_3D", url_3D);
        json.put("organization_3D", organization_3D);
        json.put("organizationName_3D", organizationName_3D);
        json.put("org_id", org_id);
        PlatformModePO po = new PlatformModePO();
        User user = po.getUser();
        redisUtils.set(user.getId(), json);
        return new R();
    }

    @ApiOperation(value = "取组织机构id")
    @GetMapping(value = "/getRedis")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<Object>> getRedis() {
        List<Object> list = new ArrayList<>();
        PlatformParamPo po = new PlatformParamPo();
        User user = po.getUser();
        String id = user.getId();
        JSONObject json = redisUtils.get(id);
        if (json != null) {
            list.add(json.get("back_3D"));
            list.add(json.get("url_3D"));
            list.add(json.get("organization_3D"));
            list.add(json.get("organizationName_3D"));
            list.add(json.get("org_id"));
        } else {
            list.add("");
            list.add("");
            list.add("");
            list.add("");
            list.add("");
        }
        return new R(list);
    }


}
